<template>
  <div class="app-container">
    <!-- 头部搜索区域 -->
    <div class="search_and_new">
      <div class="put">
        <el-input v-model="searchForm.formName" placeholder="请输入表单名称" style="width:200px;" />
      </div>
      <div class="put">
        <el-input v-model="searchForm.formCategory" placeholder="请输入表单分类" style="width:200px;" />
      </div>
      <div class="new_btn">
        <el-button type="primary" icon="el-icon-plus" size="mini" @click="handleAdd">添加表单</el-button>
      </div>
      <el-button type="primary" size="mini" icon="el-icon-search" @click="getFormList">搜索</el-button>
      <el-button type="primary" size="mini" @click="handleRefresh">刷新</el-button>
      <el-button type="primary" size="mini" @click="handleReset">重置</el-button>
    </div>

    <!-- 表格区域 -->
    <el-table v-loading="listLoading" :data="formList" stripe style="width:100%">
      <el-table-column type="index" width="60" label="序号" />
      <el-table-column prop="formName" label="表单名称" width="150" />
      <el-table-column prop="formCode" label="表单编码" width="150" />
      <el-table-column prop="formCategory" label="表单分类" width="120" />
      <el-table-column prop="dataTableName" label="数据表名" width="150" />
      <el-table-column prop="formDesc" label="表单描述" min-width="200" show-overflow-tooltip />
      <el-table-column label="状态" width="100">
        <template slot-scope="scope">
          <el-switch
            v-model="scope.row.status"
            :active-value="1"
            :inactive-value="0"
            active-color="#13ce66"
            @change="handleStatusChange(scope.row)"
          />
        </template>
      </el-table-column>
      <el-table-column prop="sortOrder" label="排序" width="80" />
      <el-table-column prop="createTime" label="创建时间" width="180" />
      <el-table-column label="操作" width="320" fixed="right">
        <template slot-scope="scope">
          <el-button type="primary" size="mini" @click="handleManageFields(scope.row)">字段管理</el-button>
          <el-button type="success" size="mini" @click="handleEdit(scope.row)">编辑</el-button>
          <el-button type="danger" size="mini" @click="handleDelete(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 分页区 -->
    <div class="paging_device">
      <el-pagination
        :current-page="pageNum"
        :page-sizes="[10, 20, 30, 40]"
        :page-size="pageSize"
        background
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
    </div>

    <!-- 添加/编辑表单弹窗 -->
    <el-dialog :title="dialogTitle" :visible.sync="dialogVisible" :close-on-click-modal="false" width="40%">
      <el-form ref="formConfigForm" :model="formConfigForm" :rules="formRules" label-width="120px">
        <el-form-item label="表单名称" prop="formName">
          <el-input v-model="formConfigForm.formName" placeholder="请输入表单名称" />
        </el-form-item>
        <el-form-item label="表单编码" prop="formCode">
          <el-input v-model="formConfigForm.formCode" placeholder="请输入表单编码（唯一）" :disabled="isEdit" />
        </el-form-item>
        <el-form-item label="表单分类" prop="formCategory">
          <el-input v-model="formConfigForm.formCategory" placeholder="请输入表单分类" />
        </el-form-item>
        <el-form-item label="数据表名" prop="dataTableName">
          <el-input v-model="formConfigForm.dataTableName" placeholder="请输入关联的数据表名" />
        </el-form-item>
        <el-form-item label="表单描述" prop="formDesc">
          <el-input v-model="formConfigForm.formDesc" type="textarea" :rows="3" placeholder="请输入表单描述" />
        </el-form-item>
        <el-form-item label="状态" prop="status">
          <el-radio-group v-model="formConfigForm.status">
            <el-radio :label="1">启用</el-radio>
            <el-radio :label="0">禁用</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="排序号" prop="sortOrder">
          <el-input-number v-model="formConfigForm.sortOrder" :min="0" />
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="handleSubmit">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { getFormConfigPage, createFormConfig, updateFormConfig, deleteFormConfig, updateFormStatus } from '@/api/form'

export default {
  name: 'FormConfigManage',
  data() {
    return {
      searchForm: {
        formName: '',
        formCategory: ''
      },
      listLoading: false,
      formList: [],
      pageNum: 1,
      pageSize: 10,
      total: 0,
      dialogVisible: false,
      dialogTitle: '添加表单',
      isEdit: false,
      formConfigForm: {
        formName: '',
        formCode: '',
        formCategory: '',
        dataTableName: '',
        formDesc: '',
        status: 1,
        sortOrder: 0
      },
      formRules: {
        formName: [
          { required: true, message: '请输入表单名称', trigger: 'blur' }
        ],
        formCode: [
          { required: true, message: '请输入表单编码', trigger: 'blur' }
        ],
        dataTableName: [
          { required: true, message: '请输入数据表名', trigger: 'blur' }
        ]
      }
    }
  },
  mounted() {
    this.getFormList()
  },
  methods: {
    // 获取表单列表
    getFormList() {
      this.listLoading = true
      const params = {
        pageNum: this.pageNum,
        pageSize: this.pageSize,
        formName: this.searchForm.formName,
        formCategory: this.searchForm.formCategory
      }
      getFormConfigPage(params).then(res => {
        if (res.code === 200) {
          this.formList = res.result.records
          this.total = res.result.total
        }
        this.listLoading = false
      }).catch(() => {
        this.listLoading = false
      })
    },
    // 分页大小变化
    handleSizeChange(val) {
      this.pageSize = val
      this.pageNum = 1
      this.getFormList()
    },
    // 当前页变化
    handleCurrentChange(val) {
      this.pageNum = val
      this.getFormList()
    },
    // 刷新
    handleRefresh() {
      this.getFormList()
    },
    // 重置
    handleReset() {
      this.searchForm = {
        formName: '',
        formCategory: ''
      }
      this.pageNum = 1
      this.getFormList()
    },
    // 添加表单
    handleAdd() {
      this.dialogTitle = '添加表单'
      this.isEdit = false
      this.formConfigForm = {
        formName: '',
        formCode: '',
        formCategory: '',
        dataTableName: '',
        formDesc: '',
        status: 1,
        sortOrder: 0
      }
      this.dialogVisible = true
    },
    // 编辑表单
    handleEdit(row) {
      this.dialogTitle = '编辑表单'
      this.isEdit = true
      this.formConfigForm = {
        id: row.id,
        formName: row.formName,
        formCode: row.formCode,
        formCategory: row.formCategory,
        dataTableName: row.dataTableName,
        formDesc: row.formDesc,
        status: row.status,
        sortOrder: row.sortOrder
      }
      this.dialogVisible = true
    },
    // 提交
    handleSubmit() {
      this.$refs.formConfigForm.validate(valid => {
        if (valid) {
          if (this.isEdit) {
            updateFormConfig(this.formConfigForm.id, this.formConfigForm).then(res => {
              if (res.code === 200) {
                this.$message.success('编辑成功')
                this.dialogVisible = false
                this.getFormList()
              }
            })
          } else {
            createFormConfig(this.formConfigForm).then(res => {
              if (res.code === 200) {
                this.$message.success('添加成功')
                this.dialogVisible = false
                this.getFormList()
              }
            })
          }
        }
      })
    },
    // 状态变更
    handleStatusChange(row) {
      updateFormStatus(row.id, row.status).then(res => {
        if (res.code === 200) {
          this.$message.success('状态更新成功')
        } else {
          row.status = row.status === 1 ? 0 : 1
        }
      }).catch(() => {
        row.status = row.status === 1 ? 0 : 1
      })
    },
    // 删除
    handleDelete(row) {
      this.$confirm('确定要删除该表单吗？删除后对应的字段配置也会被删除', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        deleteFormConfig(row.id).then(res => {
          if (res.code === 200) {
            this.$message.success('删除成功')
            this.getFormList()
          }
        })
      })
    },
    // 字段管理
    handleManageFields(row) {
      this.$router.push({
        path: '/form/field',
        query: { formId: row.id, formName: row.formName, formCode: row.formCode }
      })
    }
  }
}
</script>

<style scoped>
.app-container {
  padding: 20px;
}

.search_and_new {
  display: flex;
  align-items: center;
  gap: 10px;
  margin-bottom: 20px;
}

.put {
  margin-right: 10px;
}

.paging_device {
  margin-top: 20px;
  display: flex;
  justify-content: flex-end;
}
</style>
